package cn.stronglink.asset.terminal.bjc.transfer.service;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.model.ItAssetExtPosition;
import cn.stronglink.asset.model.PullOff;
import cn.stronglink.asset.model.PullOffDetail;
import cn.stronglink.asset.model.PutOn;
import cn.stronglink.asset.model.PutOnDetail;
import cn.stronglink.asset.model.Transfer;
import cn.stronglink.asset.model.TransferDetail;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetExtPositionMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.assetmanage.pulloff.mapper.PullOffMapper;
import cn.stronglink.asset.module.assetmanage.pulloff.service.PullOffService;
import cn.stronglink.asset.module.assetmanage.puton.mapper.PutOnMapper;
import cn.stronglink.asset.module.assetmanage.puton.service.PutOnService;
import cn.stronglink.asset.module.assetmanage.transfer.mapper.TransferMapper;
import cn.stronglink.asset.module.assetmanage.transfer.vo.OriginalAssetInfo;
import cn.stronglink.asset.terminal.bjc.transfer.mapper.TransferApiMapper;
import cn.stronglink.asset.terminal.bjc.transfer.vo.TransferApiVo;
import cn.stronglink.asset.terminal.bjc.transfer.vo.TransferDetailApiVo;

@Service
@Transactional(rollbackFor = Exception.class)
public class TransferApiService {
	@Autowired
	private TransferMapper transferMapper;
	@Autowired
	private TransferApiMapper transferApiMapper;
	@Autowired
	private PullOffMapper pullOffMapper;
	@Autowired
	private PutOnMapper putOnMapper;

	public void createTransfer(Transfer transfer) {
		// 迁移单
		transfer.setId(IdWorker.getId());
		transfer.setStatus(3);
		transfer.setOperatorBy(transfer.getCreateBy());
		transfer.setExecuteTime(new Date());
		transfer.setCreateTime(new Date());
		transferMapper.insertTransfer(transfer);//新增迁移主表

		if (transfer.getTransferDetailList() != null && transfer.getTransferDetailList().size() > 0) {
			Set<Long> originalRoomIdSet = new HashSet<Long>();//原机房ID
			Set<Long> targetRoomIdSet = new HashSet<Long>();//目标机房ID
			for (TransferDetail detail : transfer.getTransferDetailList()) {
				OriginalAssetInfo assetInfo = transferMapper.getAsset(Long.valueOf(detail.getAssetId_()));//通过资产id 找到原位置信息
				if (assetInfo != null) {
					detail.setOriginalManageBranchId(assetInfo.getOriginalManageBranchId());
					detail.setOriginalUseBranchId(assetInfo.getOriginalUseBranchId());
					detail.setOriginalEndUser(assetInfo.getOriginalEndUser());
					detail.setOriginalRoomId(assetInfo.getOriginalRoomId());
					detail.setOriginalRackId(assetInfo.getOriginalRackId());
					detail.setOriginalStart(assetInfo.getOriginalStart());
					detail.setOriginalEnd(assetInfo.getOriginalEnd());
					originalRoomIdSet.add(assetInfo.getOriginalRoomId());
				}

				targetRoomIdSet.add(detail.getTargetRoomId());
			}

			// 创建下架单
			Map<Long, PullOff> pullOffMap = new HashMap<>();
			for (Long originalRoomId : originalRoomIdSet) {
				// 下架单
				PullOff pullOff = new PullOff();
				pullOff.setId(IdWorker.getId());
				pullOff.setExecuteTime(transfer.getExecuteTime());
				pullOff.setOperatorBy(transfer.getOperatorBy());
				pullOff.setRoomId(originalRoomId);
				pullOff.setType(2);
				pullOff.setStatus(1);
				pullOff.setRemark(transfer.getRemark());
				pullOff.setCreateTime(new Date());
				pullOff.setCreateBy(transfer.getCreateBy());
				pullOffMap.put(originalRoomId, pullOff);
				pullOffMapper.insertPullOff(pullOff);

				for (TransferDetail detail : transfer.getTransferDetailList()) {

					// 下架单详情
					PullOffDetail pullOffDetail = new PullOffDetail();
					pullOffDetail.setId(IdWorker.getId());
					pullOffDetail.setPullOffId(pullOff.getId());
					pullOffDetail.setAssetId(Long.valueOf(detail.getAssetId_()));
					pullOffDetail.setStatus(2);
					pullOffDetail.setCreateTime(new Date());
					pullOffDetail.setCreateBy(transfer.getCreateBy());
					pullOffMapper.insertPullOffDetail(pullOffDetail);

				}
			}

			// 创建上架单
			Map<Long, PutOn> putOnMap = new HashMap<>();
			for (Long targetRoomId : targetRoomIdSet) {
				PutOn putOn = new PutOn();
				putOn.setId(IdWorker.getId());
				putOn.setExecuteTime(transfer.getExecuteTime());
				putOn.setOperatorBy(transfer.getOperatorBy());
				putOn.setRoomId(targetRoomId);
				putOn.setType(2);
				putOn.setStatus(1);
				putOn.setRemark(transfer.getRemark());
				putOn.setCreateTime(new Date());
				putOn.setCreateBy(transfer.getCreateBy());
				putOnMap.put(targetRoomId, putOn);
				putOnMapper.insertPutOn(putOn);
				for (TransferDetail detail : transfer.getTransferDetailList()) {
					// 上架单详情

					PutOnDetail putOnDetail = new PutOnDetail();
					putOnDetail.setId(IdWorker.getId());
					putOnDetail.setPutOnId(putOn.getId());
					putOnDetail.setAssetId(Long.valueOf(detail.getAssetId_()));
					putOnDetail.setRoomId(detail.getTargetRoomId().toString());
					putOnDetail.setRackId(detail.getTargetRackId().toString());
					putOnDetail.setStartU(detail.getTargetStart());
					putOnDetail.setStatus(2);
					putOnDetail.setCreateBy(transfer.getCreateBy());
					putOnDetail.setCreateTime(new Date());
					putOnMapper.insertPutOnDetail(putOnDetail);

				}
			}

			for (TransferDetail detail : transfer.getTransferDetailList()) {
				detail.setTransferId(transfer.getId());
				detail.setAssetId(Long.valueOf(detail.getAssetId_()));
				detail.setPullOffId(pullOffMap.get(detail.getOriginalRoomId()).getId());
				detail.setPutOnId(putOnMap.get(detail.getTargetRoomId()).getId());
				detail.setStatus(1);
				detail.setId(IdWorker.getId());
				detail.setCreateBy(transfer.getCreateBy());
				detail.setCreateTime(new Date());
				transferMapper.insertTransferDetail(detail);
			}
		}
	}

	public List<TransferApiVo> qryTransferList(Map<String, Object> params) {
		List<TransferApiVo> list = transferApiMapper.qryTransferList(params);
		if (list != null && list.size() > 0) {
			for (TransferApiVo transferApiVo : list) {
				List<TransferDetailApiVo> detailList = transferApiMapper
						.qryTransferDetailListApi(transferApiVo.getId());
				transferApiVo.setDetailList(detailList);
			}
		}
		return list;
	}

}
